using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class StoresRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class StoresDbo : DatabaseObject
        {
            public StoresDbo()
            {
                Name = "Stores";
                GeneratedClassName = "Stores";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "smallint", IsIdentity = true, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CountryId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CountryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Code", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Code" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Name", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Name" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Region", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Region" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Subregion", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Subregion" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "City", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "City" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "State", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "State" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SEL", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "SEL" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ShoppingOccasion", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "ShoppingOccasion" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Size", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Size" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Address", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 400, Precision = 0, Scale = 0, GeneratedPropertyName = "Address" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Phone", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Phone" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Fax", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Fax" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Format", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Format" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PBL", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "PBL" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Mail", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Mail" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Contact", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Contact" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SiteLocation", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "SiteLocation" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ShopType", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "ShopType" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Layout", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Layout" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "OpeningHours", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "OpeningHours" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute1", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute1" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute2", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute2" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SELCustomers", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "SELCustomers" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SELLocation", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "SELLocation" });
              
            }
        }
		
		public static List<StoreRow> Select(Expression<Func<StoreRow, bool>> expression)
        {
            List<StoreRow> rows = new List<StoreRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<StoreRow>(new StoresDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<StoreRow> Select()
		{
			List<StoreRow> rows = new List<StoreRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<StoreRow>(new StoresDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<StoreRow> PagedSelect(Expression<Func<StoreRow, bool>> filterExpression, Expression<Func<StoreRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<StoreRow> rows = new List<StoreRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<StoreRow>(new StoresDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<StoreRow> StoreList)
        {
            StoreRow row;

            while (reader.Read())
            {
                row = new StoreRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					CountryId = Util.CastDbValue<int>(reader["CountryId"]),
					Code = Util.CastDbValue<string>(reader["Code"]),
					Name = Util.CastDbValue<string>(reader["Name"]),
					Region = Util.CastDbValue<string>(reader["Region"]),
					Subregion = Util.CastDbValue<string>(reader["Subregion"]),
					City = Util.CastDbValue<string>(reader["City"]),
					State = Util.CastDbValue<string>(reader["State"]),
					SEL = Util.CastDbValue<string>(reader["SEL"]),
					ShoppingOccasion = Util.CastDbValue<string>(reader["ShoppingOccasion"]),
					Size = Util.CastDbValue<string>(reader["Size"]),
					Address = Util.CastDbValue<string>(reader["Address"]),
					Phone = Util.CastDbValue<string>(reader["Phone"]),
					Fax = Util.CastDbValue<string>(reader["Fax"]),
					Format = Util.CastDbValue<string>(reader["Format"]),
					PBL = Util.CastDbValue<string>(reader["PBL"]),
					Mail = Util.CastDbValue<string>(reader["Mail"]),
					Contact = Util.CastDbValue<string>(reader["Contact"]),
					SiteLocation = Util.CastDbValue<string>(reader["SiteLocation"]),
					ShopType = Util.CastDbValue<string>(reader["ShopType"]),
					Layout = Util.CastDbValue<string>(reader["Layout"]),
					OpeningHours = Util.CastDbValue<string>(reader["OpeningHours"]),
					Attribute1 = Util.CastDbValue<string>(reader["Attribute1"]),
					Attribute2 = Util.CastDbValue<string>(reader["Attribute2"]),
					SELCustomers = Util.CastDbValue<string>(reader["SELCustomers"]),
					SELLocation = Util.CastDbValue<string>(reader["SELLocation"])
                };
                                
                StoreList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(StoreRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<StoreRow>(new StoresDbo(), row);
            }
        }

		public static void Update(StoreRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<StoreRow>(new StoresDbo(), row);
            }
        }

		public static void Delete(Expression<Func<StoreRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<StoreRow>(new StoresDbo(), expression);
            }
        }

	}
}
	
